﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Utility;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using Accounting.Library.Utility;
using System.ComponentModel;

namespace csla4Skeleton.UI.SL.TongHop.TH_NghiepVuKhac
{
    public partial class TH_NghiepVuKhac_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {
        #region ISupportNavigation Members

        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "Chung tu nghiep vu khac"; }
        }

        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
        private DM_DoiTuongTHCP_ChildWindow DoiTuongTHCP_ChildWindow;
        List<int> ListOfRowHasTKNoStartWithSix = new List<int>();
        public TH_NghiepVuKhac_MainWindow(int Khoa)
            : this()
        {
            this.Khoa = Khoa;
        }

        public TH_NghiepVuKhac_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.TongHop.TH_NghiepVuKhac.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource TH_NghiepVuKhacViewModelSource = (System.Windows.Data.CollectionViewSource)this.Resources["TH_NghiepVuKhacViewModelSource"];
                if (Khoa == null)
                {
                    TH_NghiepVuKhac_ViewModel TH_NghiepVuKhacViewModel = new TH_NghiepVuKhac_ViewModel();
                    TH_NghiepVuKhacViewModel.ChildList_AddedNew += new TH_NghiepVuKhac_ViewModel.ChildList_AddedNew_EventHandler(TH_NghiepVuKhacViewModel_ChildList_AddedNew);
                    TH_NghiepVuKhacViewModel.TKNoIdStartWithSix += new TH_NghiepVuKhac_ViewModel.TKNoIdStartWithSix_EventHandler(OnTKNoIdStartWithSix);
                    TH_NghiepVuKhacViewModel.DoiTuongTHCPIDChanged += new TH_NghiepVuKhac_ViewModel.DoiTuongTHCPIDChanged_EventHandler(OnDoiTuongTHCPIDChanged);
                    TH_NghiepVuKhacViewModelSource.Source = new List<object> { TH_NghiepVuKhacViewModel };
                }
                else
                {
                    TH_NghiepVuKhac_ViewModel TH_NghiepVuKhacViewModel = new TH_NghiepVuKhac_ViewModel((int)Khoa);
                    TH_NghiepVuKhacViewModel.ChildList_AddedNew += new TH_NghiepVuKhac_ViewModel.ChildList_AddedNew_EventHandler(TH_NghiepVuKhacViewModel_ChildList_AddedNew);
                    TH_NghiepVuKhacViewModel.TKNoIdStartWithSix += new TH_NghiepVuKhac_ViewModel.TKNoIdStartWithSix_EventHandler(OnTKNoIdStartWithSix);
                    TH_NghiepVuKhacViewModel.DoiTuongTHCPIDChanged += new TH_NghiepVuKhac_ViewModel.DoiTuongTHCPIDChanged_EventHandler(OnDoiTuongTHCPIDChanged);
                    TH_NghiepVuKhacViewModelSource.Source = new List<object> { TH_NghiepVuKhacViewModel };

                    this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 1].IsVisible = true;
                    this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 2].IsVisible = true;
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_TatCa);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_ChiTiet_ListViewModelSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel taiKhoanChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_ChiTiet_ListViewModelSource.Source = new List<object> { taiKhoanChiTiet_ListViewModel };


                this.TH_NghiepVuKhac_ChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TH_NghiepVuKhac_ChiTietGridView);
                this.TaxDetailsGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TaxDetailsGridView);
                
                TH_NghiepVuKhac_DienGiai_TextBox.Focus();
            }
        }

        #endregion

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TH_NghiepVuKhac_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.OrderDetailsTab.IsSelected = true;
                }
            }
            e.Handled = true;
        }
        void AddItems()
        {
            if (this.OrderDetailsTab.IsSelected)
            {
                AddOrderDetailsItem();
            }
        }
        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, null);
        }
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[1]);
                grid.Focus();
                grid.BeginEdit();
            }));
        }

        private RadGridView GetActiveGridView()
        {
            if (this.OrderDetailsTab.IsSelected)
            {
                return TH_NghiepVuKhac_ChiTietGridView;
            }

            return null;
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            TH_NghiepVuKhac_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            AddItems();
        }
        #endregion

        #region View Model Related Methods
        private TH_NghiepVuKhac_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TH_NghiepVuKhacViewModelSource = (System.Windows.Data.CollectionViewSource)this.Resources["TH_NghiepVuKhacViewModelSource"];
            return ((List<object>)TH_NghiepVuKhacViewModelSource.Source)[0] as TH_NghiepVuKhac_ViewModel;
        }
        private void AddOrderDetailsItem()
        {
            GetViewModel().AddOrderDetailsItem();
        }

        void TH_NghiepVuKhacViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TH_NghiepVuKhac_ChiTiets")
            {
                this.TH_NghiepVuKhac_ChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

        void OnTKNoIdStartWithSix(object sender, TKNoIdStartWithSixEventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ChiTiet_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = ((List<object>)dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            IList<DM_TaiKhoanInfo> listTaiKhoanNo = dm_TaiKhoanNo_ChiTiet_ListViewModel.Model.getListOfInfo(); ;

            while (ListOfRowHasTKNoStartWithSix.Count != 0)
            {
                ListOfRowHasTKNoStartWithSix.RemoveAt(0);
            }
            int index = 0;
            foreach (TH_NghiepVuKhac_ChiTietChild item in this.GetViewModel().Model.TH_NghiepVuKhac_ChiTiets)
            {
                foreach (DM_TaiKhoanInfo itemTaiKhoan in listTaiKhoanNo)
                {
                    if (item.TkNoId == itemTaiKhoan.Khoa)
                    {
                        if (itemTaiKhoan.Ma.StartsWith("6"))
                        {
                            ListOfRowHasTKNoStartWithSix.Add(index);
                            break;
                        }
                        else
                        {
                            item.DoiTuongTHCPId = null;
                            item.HangHoa = null;
                        }
                    }
                }
                index++;
            }
            if (ListOfRowHasTKNoStartWithSix.Count != 0)
            {
                this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 1].IsVisible = true;
                this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 2].IsVisible = true;

            }
            else
            {
                this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 1].IsVisible = false;
                this.TH_NghiepVuKhac_ChiTietGridView.Columns[this.TH_NghiepVuKhac_ChiTietGridView.Columns.Count - 2].IsVisible = false;
            }
        }

        public void OnDoiTuongTHCPIDChanged(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            bool flag = false;
            foreach (int index in ListOfRowHasTKNoStartWithSix)
            {
                if (index == e.Index)
                {
                    flag = true;
                    break;
                }

            }
            if (this.GetViewModel().Model.TH_NghiepVuKhac_ChiTiets[e.Index].DoiTuongTHCPId != null && flag == true)
            {
                DoiTuongTHCP_ChildWindow = new DM_DoiTuongTHCP_ChildWindow((int)this.GetViewModel().Model.TH_NghiepVuKhac_ChiTiets[e.Index].DoiTuongTHCPId, e.Index);
                DoiTuongTHCP_ChildWindow.Show();
                DoiTuongTHCP_ChildWindow.Completed += new DM_DoiTuongTHCP_ChildWindow.DoiTuongTHCPId_EventHandler(OnCompleted);
            }
            else
            {
                this.GetViewModel().Model.TH_NghiepVuKhac_ChiTiets[e.Index].DoiTuongTHCPId = null;
            }
        }
        public void OnCompleted(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            IList<TH_NghiepVuKhac_ChiTietChild> list = this.GetViewModel().Model.TH_NghiepVuKhac_ChiTiets;
            list[e.Index].HangHoa = DoiTuongTHCP_ChildWindow.SelectedThanhPham;
            DoiTuongTHCP_ChildWindow.Close();
        }

        
        #endregion

        private void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            this.TH_NghiepVuKhac_ChiTietGridView.Items.Remove((TH_KyKetChuyen_ChiTietChild)this.TH_NghiepVuKhac_ChiTietGridView.SelectedItem);
        }

        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        private void CreateNewItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().NewTH_NghiepVuKhac_ViewModel();
        }

        private void DeleteVoucherItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().DeleteTH_NghiepVuKhac();
        }

        private void TH_NghiepVuKhac_ChiTietGridView_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
        {

        }
    }
}
